﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Revis.VClinicNet.UI.External.Client.Service;
using MvvmFoundation.Wpf;
using System.Windows.Input;
using System.Threading;
using System.Windows.Data;
using System.Windows.Controls;

namespace Revis.VClinicNet.UI.External.Client.ViewModel
{
    public class ChangePasswordConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Func<string, PasswordBox> FindPasswordBoxByName = (name) => {
                return (PasswordBox)values.Single(obj => {
                    PasswordBox pass = (obj as PasswordBox);
                    return pass != null && pass.Name == name ? true : false;
                });
            };
            return new PlainChangePasswordProvider(
                new PasswordBoxPlainPasswordProvider(FindPasswordBoxByName("OldUserPassword")),
                new PasswordBoxPlainPasswordProvider(FindPasswordBoxByName("NewUserPassword")));
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public interface IPlainChangePasswordProvider
    {
        IPlainPasswordProvider OldPasswordProvider { get; }
        IPlainPasswordProvider NewPasswordProvider { get; }
    }

    public class PlainChangePasswordProvider : IPlainChangePasswordProvider
    {
        public IPlainPasswordProvider OldPasswordProvider { get; protected set; }
        public IPlainPasswordProvider NewPasswordProvider { get; protected set; }

        public PlainChangePasswordProvider(
            IPlainPasswordProvider oldPass,
            IPlainPasswordProvider newPass)
        {
            OldPasswordProvider = oldPass;
            NewPasswordProvider = newPass;
        }
    }

    public class ChangePasswordViewModel : WorkspaceControlViewModelBase<View.ChangePassword>
    {
        public ChangePasswordViewModel() { }

        public ChangePasswordViewModel(string displayName, IPatientService ips) :
            base(displayName, ips)
        {
        }

        public override void Run()
        {

        }

        public void Request(IPlainChangePasswordProvider providers)
        {
            ChangeState("Próba zmiany hasła", RequestState.Requesting);
            _patientService.BeginChangePassword(
                providers.OldPasswordProvider.Password,
                providers.NewPasswordProvider.Password,
                new AsyncCallback(RequestEnded), null);
        }

        public void RequestEnded(IAsyncResult result)
        {
            try {
                bool isSuccessful = _patientService.EndChangePassword(result);
                if (isSuccessful == true) {
                    ChangeState("Hasło zostało zamienione", RequestState.Success);
                } else {
                    ChangeState("Próba zmiany hasła zakończyła się niepowodzeniem", RequestState.Failed);
                }
            } catch (Exception) {
                ChangeState("Wystąpił poważny błąd podczas zmiany hasła", RequestState.Faulted);
            }
        }

        private RelayCommand<IPlainChangePasswordProvider> _changePasswordCommand = null;
        public ICommand ChangePasswordCommand
        {
            get
            {
                if (_changePasswordCommand == null) {
                    _changePasswordCommand = new RelayCommand<IPlainChangePasswordProvider>(Request);
                }
                return _changePasswordCommand;
            }
        }
    }
}
